#ifndef DATABASEQUERY_H
#define DATABASEQUERY_H

#include <QSqlQuery>
#include <QVariant>
#include <QStringList>
#include "database.h"
#include "common/gui/mymessagedialog.h"
#include <QApplication>
#include <QThread>
#include <QDebug>
/*
class DataBaseData
{
public:
    QString mStrName;
    QVariant mContent;

    DataBaseData(QString str,QVariant content)
    {
        mStrName = str;
        mContent = content;
    }
};
*/
class SignSender:public QObject
{
    Q_OBJECT
public:
//    static SignSender *getObj(){
//        if(mObj==NULL){
//            mObj = new SignSender();
//        }
//        return mObj;
//    }
    SignSender(QObject *parent=NULL):QObject(parent){

    }
    void sendErrorSignal(const QString &title,const QString &text)
    {
       // qDebug()<<"error send in thread"<<QThread::currentThread()->currentThreadId();
        emit error(title,text);
    }
private:


signals:
    void error(const QString &title,const QString &text);

private:
   // static SignSender *mObj;
};

class SignReceiver:public QObject
{
    Q_OBJECT
public:
    static SignReceiver *getObj(){
        if(mObj==NULL){
            mObj = new SignReceiver();
            mObj->moveToThread(qApp->thread());
        }
        return mObj;
    }
private:
    SignReceiver(QObject *parent=NULL):QObject(parent){
        //connect(SignSender::getObj(),SIGNAL(error(QString,QString)),this,SLOT(showError(QString,QString)));
    }
public slots:
    void showError(const QString &title,const QString &text)
    {
        MyMessageDialog::warning(0,title,text,0);
    }

private:
    static SignReceiver *mObj;

};




class DataBaseQuery : public QSqlQuery
{
public:

    explicit DataBaseQuery(QSqlDatabase db=DataBase::getDataBase(),bool showError=true);
    ~DataBaseQuery();
    bool containColumn(const QString &tabName,const QString &cloumn);
    bool addColumn(const QString &tabName,const QString &column,const QString &option);
    //void insert(QString tabName,QList<DataBaseData> &dataList);
    bool insert(const QString &tabName,const QMap<QString,QVariant> &dataMap);
    void getDataFromTable(const QString &tabName,QList< QMap<QString,QVariant> > *list);
    bool insert(const QString &tabName,const QList< QMap<QString,QVariant> > *list);
    static void copy(const QString &tabName,DataBaseQuery *qyDest,const DataBaseQuery *qySource);
    //bool selectTable(QString tabName,const QStringList *keyList=NULL,const QMap<QString,QVariant>*where = NULL,QString shortItem="",bool shortdesc=0);
    bool selectTable(const QString &tabName,const QStringList *keyList=NULL,const QString &option="",const QString &shortItem="",bool shortdesc=0);
    //bool selectTable(const QString &tabName,const QStringList *keyList=NULL,const QStringList &optionList=QStringList(),const QString &shortItem="",bool shortdesc=0);

    bool selectTable(const QStringList *tabList,const QStringList *keyList=NULL,const QString &option="");
   // bool selectTable(const QString &tabList,const QString &key,const QString &content,const QStringList *keyList=NULL);
    bool leftJoin(const QString &tabName1, const QString &tabName2, const QStringList *keyList,const QString &option);
    bool innerJoin(QString tabName, const QStringList *keyList, const QStringList &joinOption);
    bool deleteItem(const QString &tabName,const QString &option="");

    bool deleteItem(const QString &tabName, const QMap<QString ,QVariant> &map);
    bool deleteItem(const QString &tabName,const QString &key,const QVariant &data);
    bool updateItem(const QString &tabName,const QMap<QString,QVariant> &dataMap,const QString &option="");
    bool updateItem(const QString &tabName,const QMap<QString,QString> &dataMap,const QString &option="");
    bool isItemExist(const QString &tabName, const QString &item, const QVariant &content);
    bool updateItem(const QString &tabName, const QString &fieldName, const QString &now, const QMap<QString, QVariant> &dataMap_new);
    bool resetTable(const QString &tabName);
    bool deleteTable(const QString &tabName);
    bool createIndex(const QString &tabName,const QString &fieldName,const QString &indexName="");
    bool exec(const QString &str);
    int size();

    bool copyTab(const QString &destTab, const QString &srcTab);
    void copy(const QString &tabName,const QString &qyDest,const QString &option="");
     void move(const QString &dest, const QString &src, const QString &option);
    //void updata(QString tabName,QString str,QVariant var);

    bool resetAllTable();
    static QString indexName(const QString &name);
    bool innerJoin(const QString &tabName,
                   const QStringList *itemList,
                   QMap<QString, QString> &joinOption,
                   const QString &option,
                   const QString &shortOption
                   );
    bool leftJoin(const QString &tabName,
                   const QStringList *itemList,
                   QMap<QString, QString> &joinOption,
                   const QString &option,
                   const QString &shortOption =""
                   );
    bool exec();
    void setErrHandleSlot(const QObject *receiver, const char *member);
private:
    SignSender *mSignSender;
};

#endif // DATABASEQUERY_H
